package mytool;
/*
 * 给定任意时间的定时任务
 */
import java.util.Calendar;
import java.util.Formatter;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JOptionPane;

public class Alarm {
	public static void main(String[] args) throws InterruptedException {
	}

	public static void backup() {
		Calendar cal = Calendar.getInstance();
		Formatter fmt = new Formatter();
		String weekday = fmt.format("%tA", cal).toString();
		if(weekday.equals("星期二")||weekday.equals("星期五"))
			JOptionPane.showMessageDialog(null,"backup data");
	}

	public static void dingshi() {
		final String dingshi = JOptionPane.showInputDialog("请输入定时时间(24小时制，hh:mm)");
		
		Calendar cal = Calendar.getInstance();
		
		class Task extends TimerTask{
			Calendar cal;
			public Task(Calendar cal){
				this.cal=cal;
			}
			public void run() {
				Formatter fmt = new Formatter();
				String time = fmt.format("%tR", cal).toString();
				System.out.println(time);
				if(time.equals(dingshi)){
					JOptionPane.showMessageDialog(null, "时间到了！");
					System.exit(0);
				}
				cal.add(Calendar.SECOND, 1);
			}
		}
		Task task = new Task(cal);
		Timer timer = new Timer();
		timer.scheduleAtFixedRate(task, 0, 1000);
	}

	public static void daojishi() {
		String daojishi = JOptionPane.showInputDialog("请输入倒计时时长，单位分钟");
		int time = Integer.parseInt(daojishi);
		final Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(-8*60*60*1000);
		cal.add(Calendar.MINUTE, time);
		
		class Task extends TimerTask{
			
			public void run() {
//				System.out.printf("%tT%n",cal);
				String s = cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE)+":"+cal.get(Calendar.SECOND);
				cal.add(Calendar.SECOND, -1);
				if("0:0:0".equals(s)){
					JOptionPane.showMessageDialog(null, "时间到了！");
					System.exit(0);
				}
			}
		}
		
		Task task = new Task();
		Timer timer = new Timer();
		timer.scheduleAtFixedRate(task, 0, 1000);
	}

	public static void reminder(int minutes) throws InterruptedException {
		Calendar time = Calendar.getInstance();
		System.out.println("现在是" + time.get(Calendar.HOUR_OF_DAY) + ":"
				+ time.get(Calendar.MINUTE) + ":" + time.get(Calendar.SECOND));
		int count = minutes / 5;
		long sleepTime = 5 * 60 * 1000;
		for (int i = 0; i < count; i++) {
			Thread.sleep(sleepTime);
			System.out.println("已过了" + ((i + 1) * 5) + "分钟");
		}
		Thread.sleep(minutes % 5 * 60 * 1000);
		time = Calendar.getInstance();
		System.out.println("现在是" + time.get(Calendar.HOUR_OF_DAY) + ":"
				+ time.get(Calendar.MINUTE) + ":" + time.get(Calendar.SECOND));
		JOptionPane.showMessageDialog(null, "时间到了！");
	}
}
